package org.example.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.Dao.UserDao;
import org.example.Pojo.User;
import org.example.Service.UserService;
import org.example.Utils.UnionResult;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;
    @Override
    public UnionResult Register(User user) {
        if (!(user.getLoginaccount()!=null && user.getLoginpassword() != null)){
            return UnionResult.Failure("账户名与密码是必须的");
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("loginaccount", user.getLoginaccount());
        if (userDao.selectOne(queryWrapper) != null) {
            return UnionResult.Failure("该用户已存在");
        }else{
            int result = userDao.insert(user);
            if (result == 1) {
                return UnionResult.Success();
            }else{
                return UnionResult.Failure("未知错误");
            }
        }
    }

    @Override
    public UnionResult Login(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("loginaccount", user.getLoginaccount());
        if (userDao.selectOne(queryWrapper) != null) {
            queryWrapper.eq("loginpassword", user.getLoginpassword());
            User user1 = userDao.selectOne(queryWrapper);

            if (user1 != null) {
                user1.setLoginpassword(null);
                return UnionResult.Success("登录成功",user1);
            }else{
                return UnionResult.Failure("登录失败 请检查账户密码");
            }
        }else{
            return UnionResult.Failure("不存在的账户名");
        }
    }

    @Override
    public UnionResult UpdateAvatar(MultipartFile file, User user) throws IOException {
        File dir = new File("src/main/resources/imgs");
        String fileid = UUID.randomUUID().toString().replace("-", "");
        String fileExtension = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")).toLowerCase();
        File targetFile = new File(dir.getCanonicalPath(), fileid+fileExtension);
        user.setAvatarlink(fileid+fileExtension);
        try {
            file.transferTo(targetFile);
            return this.UpdateInfo(user);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public UnionResult UpdateInfo(User user) {
        if (userDao.updateById(user)>0){
            return UnionResult.Success("用户更新成功");
        }else{
            return UnionResult.Failure("未知错误");
        }
    }

    @Override
    public UnionResult UpdatePassword(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", user.getUid());
        queryWrapper.eq("loginpassword", user.getLoginpassword().split("\\|")[0]);
        Integer counts = userDao.selectCount(queryWrapper);
        if (counts>0){
            user.setLoginpassword(user.getLoginpassword().split("\\|")[1]);
            return this.UpdateInfo(user);
        }else{
            return UnionResult.Failure("旧密码不正确");
        }
    }

    @Override
    public UnionResult GetUserInfo(String uid) {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", uid);
        User user = userDao.selectOne(queryWrapper);
        if (user != null) {
            user.setLoginpassword(null);
            user.setLoginaccount(null);
            return UnionResult.Success("查询成功",user);
        }else{
            return UnionResult.Failure("未知错误");
        }
    }

    @Override
    public UnionResult ActiveUser(User user) {
        if (userDao.updateById(user)>0){
            return UnionResult.Success();
        }else{
            return UnionResult.Failure();
        }
    }

    @Override
    public UnionResult GetAllUsers() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        return UnionResult.Success("",userDao.selectList(queryWrapper));
    }
}
